package com.metagurukul.metaspace.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.metagurukul.metaspace.models.beans.UserWorkDetail;
import com.metagurukul.metaspace.util.Constants;

/**
 * @author ritesh
 *
 */
public class UserWorkDetailDao extends DatabaseDAO{
	/**
	 * @return
	 * @throws SQLException
	 */
	public List<UserWorkDetail> getRecordList ()
	throws SQLException {
		ArrayList<UserWorkDetail> userWorkDetailList = null;
		String query = "Select * from user_work_detail";
		try {
			getConnection();
			ps = con.prepareStatement(query);
			/**
			 * set variables in prepared statement
			 * 
			 */
			rs = ps.executeQuery();
			userWorkDetailList = new ArrayList<UserWorkDetail>();
			while (rs.next()) {
				UserWorkDetail userWorkDetail = fillBean();
				userWorkDetailList.add(userWorkDetail);
			}
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		return userWorkDetailList;
		
	}
	
	/**
	 * @return
	 * @throws SQLException
	 */
	private UserWorkDetail fillBean() throws SQLException{
		UserWorkDetail userWorkDetail = new UserWorkDetail();
		userWorkDetail.setUserId(rs.getInt("user_id"));
		userWorkDetail.setRoleId(rs.getInt("role_id"));
		userWorkDetail.setDeptId(rs.getInt("dept_id"));
		return userWorkDetail;
	}
	
	/**
	 * @param userId
	 * @return
	 * @throws SQLException
	 */
	public UserWorkDetail getRecord(int userId)
	throws SQLException {
		String query = "Select * from user_work_detail where user_id = ? ";
		try {
			getConnection();
			ps = con.prepareStatement(query);
			/**
			 * set variables in prepared statement
			 * 
			 */
			ps.setInt(1, userId);
			rs = ps.executeQuery();
			if (rs.next()) {
				
				return fillBean();
			}
			return null;
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		
		
	}
	
	/**
	 * @param userWorkDetail
	 * @return
	 * @throws SQLException
	 */
	public int addRecord(UserWorkDetail userWorkDetail) throws SQLException {
		if(!checkIsPresent(userWorkDetail.getUserId())){
			return insertRecord(userWorkDetail);
		}
		else{
			return updateRecord(userWorkDetail);
		}
	}
	
	/**
	 * @param userWorkDetail
	 * @return
	 * @throws SQLException
	 */
	private int updateRecord(UserWorkDetail userWorkDetail) throws SQLException {
		// TODO Auto-generated method stub
		getConnection();
		String query = "update user_work_detail set dept_id=?, role_id =? where user_id =?";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setInt(1, userWorkDetail.getDeptId());
			ps.setInt(2, userWorkDetail.getRoleId());
			ps.setInt(3, userWorkDetail.getUserId());
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
	}

	/**
	 * @param userWorkDetail
	 * @return
	 * @throws SQLException
	 */
	private int insertRecord(UserWorkDetail userWorkDetail) throws SQLException {
		/* get the connection */
		getConnection();
		String query = "insert into user_work_detail (user_id ,dept_id, role_id) values(?, ? , ?)";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			
			ps.setInt(1, userWorkDetail.getUserId());
			ps.setInt(2, userWorkDetail.getDeptId());
			ps.setInt(3, userWorkDetail.getRoleId());
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		
	}

	/**
	 * @param userId
	 * @return
	 * @throws SQLException
	 */
	private boolean checkIsPresent(int userId) throws SQLException {
		getConnection();
		String query = "select 1 from user_work_detail where user_id=?";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setInt(1, userId);
			rs = ps.executeQuery();
			/**
			 * if there is record in resultset then return true else return
			 * false
			 */
			return rs.next();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
	}
	
	/**to delete User Work Detail of particular user
	 * @param userId id of the user to be deleted
	 * @return integer.
	 * @throws SQLException
	 */
	public int deleteRecord(int userId) throws SQLException {
		/* get the connection */
		getConnection();
		String query = "delete from user_work_detail where user_id =?"; // query.
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setInt(1, userId);
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		
	}
}
